package com.yl.common.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yl.common.utils.LogUtil;
import com.yl.common.utils.ToastUtil;

import java.util.List;


/**
 * Created by chenjinxu on 2024/5/05.
 * 说明:
 */
public abstract class BaseActivity extends AppCompatActivity {

    LoadingDialogFragment loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideNavKey(this);
        LogUtil.getInstance().log("TAG", "--onResume " + this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.getInstance().log("TAG", "--onPause " + this);
    }

    @Override
    public void onBackPressed() {
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments.isEmpty()) {
            super.onBackPressed();
            return;
        }
        boolean intercept = false;
        for (Fragment f : fragments) {
            if (f instanceof BaseFragment) {
                intercept = ((BaseFragment) f).interceptBackPress();
                if (intercept) {
                    break;
                }
            }
        }
        if (!intercept) {
            super.onBackPressed();
        }
    }

    public static void hideNavKey(Context context) {
        //for new api versions.
        View decorView = ((Activity) context).getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }

    public void showLoading(String title) {
        if (loadingDialog != null && loadingDialog.isVisible()) {
            loadingDialog.setTitle(title);
        } else {
            loadingDialog = LoadingDialogFragment.newInstance().setTitle(title);
            loadingDialog.setCanceledOnTouchOutside(false)
                    .show(getSupportFragmentManager(), "loading");
        }
    }

    public void hideLoading() {
        try {
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showToast(String text) {
        ToastUtil.getInstance().showText(text);
    }

    public void startFragment(int containerId, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(containerId, fragment);
        transaction.commit();
    }
}
